package file_io;

import java.io.*;

public class BufferIO {
    public static void main(String[] args) throws IOException {
      /*  FileInputStream fis = new FileInputStream("src/test.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        int bRead;
         while ((bRead=bis.read())!=-1){
             System.out.print((char)bRead);
         }
        byte[] buffer =new byte[1024];
        int read = bis.read(buffer);
        System.out.print(new String(buffer,0,read));
        bis.close();*/
        /*copy01();*/
        copy02();
       /* bufferRead();*/
      /*bufferWriter();*/
    }
    static void bufferWriter(){
        try(  FileWriter fw = new FileWriter("src/test.txt",true);
              BufferedWriter bw = new BufferedWriter(fw);){
              bw.write('我');
              bw.write('爱');
              bw.write('你');
              bw.write("hello娇贵的小公主");

        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
    static void bufferRead(){
        try(  FileReader fr = new FileReader("src/test.txt");
              BufferedReader br = new BufferedReader(fr);){
            char[] chars = new char[1];
            int len;
            String s = br.readLine();
            System.out.println(s);
            while ((len=br.read(chars))!=-1){
                System.out.print(chars);
                /*System.out.println(chars);*/
            }

        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
    static void copy01(){
        long bTime=System.currentTimeMillis();
        try (InputStream is= new FileInputStream("D:\\笔记\\zwi.mp4") ;OutputStream os=new FileOutputStream("D:\\笔记\\zwip1.mp4");) {
            int read;
            while ((read=is.read())!=-1){
                os.write(read);
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
        long alltime=(System.currentTimeMillis()-bTime);
        System.out.println(alltime/1000+"秒");
    }
   static void copy02(){
        long bTime=System.currentTimeMillis();
        try (InputStream is= new FileInputStream("D:\\笔记\\zwi.mp4") ;InputStream bis=new BufferedInputStream(is);
             FileOutputStream os=new FileOutputStream("D:\\笔记\\zwip2.mp4");OutputStream bos=new BufferedOutputStream(os);) {
            int read;
            byte[] buffer=new byte[1024];
            while ((read=bis.read(buffer))!=-1){
                bos.write(buffer);
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }
        long alltime=(System.currentTimeMillis()-bTime);
        System.out.println(alltime+"秒");
    }

}
